package com.supcon.exchangedata.service.gj;

import com.supcon.exchangedata.pojo.entity.oracle.CtCar;
import com.supcon.exchangedata.pojo.entity.mysql.operation.gj.SgjjtBus;
import com.supcon.exchangedata.repository.master.MasterCtCareRepository;
import com.supcon.exchangedata.repository.slave.SlaveBusVehicleRepository;
import com.supcon.exchangedata.repository.slave4.SlaveGjCarInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;

import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toCollection;

/**
 * @program: exchange-data
 * @description:
 * @author: cherubr.Sumin
 * @create: 2021-12-15 14:20
 **/
@Service
@Slf4j
public class BusTranService {

    @Autowired
    private SlaveBusVehicleRepository slaveBusVehicleRepository;
    @Autowired
    private MasterCtCareRepository masterCtCareRepository;
    @Resource
    private SlaveGjCarInfo slaveGjCarInfo;

    /**
     * 公交车辆信息数据同步
     *
     * @throws Exception
     */
    @Transactional(rollbackFor = Exception.class)
    public void transData() throws Exception {
        masterCtCareRepository.deleteAllByGroupId(1L);

        int notExitCount = 0;
        int exitCount = 0;
        List<SgjjtBus> collect = slaveGjCarInfo.findAllBusCarStation();
        ArrayList<SgjjtBus> all = collect.stream().collect(
                collectingAndThen(
                        toCollection(() -> new TreeSet<>(comparing(SgjjtBus::getCph))), ArrayList::new));
        for (SgjjtBus bus : all) {
            CtCar ctCar = new CtCar();
          /*  CtCar ctCar = masterCtCareRepository.findByLicensePlateNum(bus.getCph());*/
            if (ctCar == null) {
                ctCar = new CtCar();
                ctCar.setCompanyId(1L);
                notExitCount++;
                log.info("新车辆号码={}", bus.getCph());
                ctCar.setGroupId(1L);
            } else {
                log.info("旧车辆号码={}", bus.getCph());
                exitCount++;
            }
            if (ctCar.getCompanyId() == null) {
                ctCar.setCompanyId(1L);
            }
            ctCar.setCarCode(bus.getClzbh());
            ctCar.setObuCode(String.valueOf(bus.getCzjbh()));
            ctCar.setLicensePlateNum(bus.getCph());
            ctCar.setUseType(bus.getYhmc());
            ctCar.setUpdateTime(LocalDateTime.now());
            ctCar.setModel(bus.getYhmc());
            ctCar.setDriverArea(bus.getXzqhmc());
            ctCar.setGroupId(1L);
            masterCtCareRepository.save(ctCar);
        }
        log.info("车辆总数={}", all.size());
        log.info("新增={}条数据，更新={}条数据,总数={}", notExitCount, exitCount, notExitCount + exitCount);
    }
}
